from flask import Blueprint, render_template, redirect, url_for, flash, request
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import login_user, logout_user, login_required, current_user
from app.models import Teacher, Student
from app import db

auth = Blueprint('auth', __name__)

@auth.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        email = request.form.get('email')
        password = request.form.get('password')
        user_type = request.form.get('user_type')
        
        if user_type == 'teacher':
            user = Teacher.query.filter_by(email=email).first()
        else:
            user = Student.query.filter_by(email=email).first()
            
        if user and check_password_hash(user.password, password):
            login_user(user)
            return redirect(url_for('main.index'))
        else:
            flash('邮箱或密码错误，请重试。')
            
    return render_template('auth/login.html')

@auth.route('/register/teacher', methods=['GET', 'POST'])
def register_teacher():
    if request.method == 'POST':
        email = request.form.get('email')
        name = request.form.get('name')
        password = request.form.get('password')
        subject = request.form.get('subject')
        
        teacher = Teacher.query.filter_by(email=email).first()
        if teacher:
            flash('该邮箱已被注册。')
            return redirect(url_for('auth.register_teacher'))
        
        new_teacher = Teacher(
            email=email,
            name=name,
            password=generate_password_hash(password, method='pbkdf2:sha256'),
            subject=subject
        )
        
        db.session.add(new_teacher)
        db.session.commit()
        
        login_user(new_teacher)
        flash('注册成功！')
        return redirect(url_for('main.index'))
        
    return render_template('auth/register_teacher.html')

@auth.route('/register/student', methods=['GET', 'POST'])
def register_student():
    if request.method == 'POST':
        email = request.form.get('email')
        name = request.form.get('name')
        password = request.form.get('password')
        grade = request.form.get('grade')
        teacher_id = request.form.get('teacher_id')
        
        student = Student.query.filter_by(email=email).first()
        if student:
            flash('该邮箱已被注册。')
            return redirect(url_for('auth.register_student'))
        
        new_student = Student(
            email=email,
            name=name,
            password=generate_password_hash(password, method='pbkdf2:sha256'),
            grade=grade,
            teacher_id=teacher_id
        )
        
        db.session.add(new_student)
        db.session.commit()
        
        login_user(new_student)
        flash('注册成功！')
        return redirect(url_for('main.index'))
        
    teachers = Teacher.query.all()
    return render_template('auth/register_student.html', teachers=teachers)

@auth.route('/logout')
@login_required
def logout():
    logout_user()
    return redirect(url_for('auth.login')) 